define(function(require, exports){
	var $ = require("jquery");
	var pubjs = require('pubjs');
	var view = require('@base/view');
	var util = require('util');
	var Edit = require('@mod/fieldEdit').base;
	var dspUtil = require('dspUtil');

	exports.base = Edit.extend({
		init:function(config){
			config = pubjs.conf(config, {
				fields: [
					{text: LANG('名称'), type: 'input', field: 'Name', validate: true},
					{
						text: LANG('策略内容'), type: 'custom',
						field: 'Strategy',
						show_block: true,
						uri: 'materialGroup/modules/edit.base', validate: true,
						config: {
							class: 'P-MaterialGroupEdit'
						}
					}
				],
				urlUpdate: 'materialGroup/update',
				urlAdd: 'materialGroup/add',
				urlInfo: 'materialGroup/info',
				urlFindRef: 'materialGroup/findRef',
				nav: 'materialGroup'
				//Title: LANG('编辑素材组')
			});

			this.Super('init', arguments);
		}
		,reset: function() {
			this.Super('reset', arguments);
			return this;
		}
		,setData: function(data) {
			if(data)
			{
				this.Super('setData', arguments);
			}

			return this;
		}
		,dataChange: function(field, v)
		{
			this.Super('dataChange', arguments);
			switch (field)
			{
				case '':
					break;
			}
		}
		,save: function (cb) {
			var self = this;
			var data = self.getData();
			if(self.validate(data)) {
				var vm = self.vm.$;
				var url = vm.isEdit ? 'urlUpdate' : 'urlAdd';
				var strategyMod = self.$.Strategy;
				strategyMod.checkWhiskyUrl(data.Strategy, function (forceUpdate) {
					data.WhiskyUrlForce = forceUpdate;
					pubjs.loading.show();
					if(vm.isEdit)
					{
						pubjs.mc.send(self.getConfig('urlFindRef'), {_id: data._id}, function (err, refs) {
							pubjs.loading.hide();
							if(err)
							{
								return pubjs.alert(err.message);
							}

							// has ref
							if(refs && refs.length)
							{
								self.showRef(refs, data.Name, function () {
									pubjs.loading.show();
									pubjs.mc.send(self.getConfig(url), data, self.afterSave.bind(self), cb)
								});
							}
							else
							{
								pubjs.loading.show();
								pubjs.mc.send(self.getConfig(url), data, self.afterSave.bind(self), cb)
							}
						});
					}
					else
					{
						pubjs.mc.send(self.getConfig(url), data, self.afterSave.bind(self), cb)
					}
				});
			}
		}
		,showRef: function (data, name, cb) {
			var self = this;
			var mod = self.$.refPop;
			self.$refCb = cb;
			if(!mod)
			{
				self.createAsync('refPop', 'materialGroup/popwin/edit.ref',{}, function (mod) {
					mod.show().setData({items: data}).setName(name);
				});
			}
			else
			{
				mod.show().setData({items: data}).setName(name);
			}
		}
		,onDialogOk: function (ev) {
			ev.from.hide();
			this.$refCb && this.$refCb();
		}
	});

	exports.popwin = exports.base.extend({
		init: function (config) {
			config = pubjs.conf(config, {
				isPopwin: true,
				module_name: 'WIDGET_UI',
				layout:  {
					module: '@base/dialog.base',
					silence: false,
					title: LANG('编辑素材组'),
					size: 'xl',
					showFoot: false,
					height: 750
				}
			});

			this.Super('init', arguments);
		}
		,afterSave: function(err, data, cb) {
			this.Super('afterSave', arguments);
			if(!err)
			{
				this.fire('materialGroupOk');
			}
		}
		,setTitle: function (v) {
			var c = this.getConfig();
			this.$[c.module_name].setTitle(v);
			return this;
		}
	});
});